<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row>
              <el-button
                icon="el-icon-plus"
                type="primary"
                size="small"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <el-table :data="list">
              <el-table-column align="center" type="index" label="序号" />
              <el-table-column align="center" label="角色名称" prop="name" />
              <el-table-column align="center" label="描述" prop="description" />
              <el-table-column align="center" label="操作">
                <template slot-scope="{ row }">
                  <el-button type="primary">分配权限</el-button>
                  <el-button type="success" @click="editRole(row)">编辑</el-button>
                  <el-button type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
            <br>
            <el-pagination
              :current-page.sync="page.page"
              :page-size="page.pagesize"
              :page-sizes="[1,2,3,4]"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"

              @current-change="currentChange"
              @size-change="sizeChange"
            />

          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>

          </el-tab-pane>
        </el-tabs>

      </el-card>
      <el-dialog
        :title="formData.id?'编辑用户':'添加用户'"
        :visible.sync="showDialog"
        width="30%"
        @close="reset"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="80px" :inline="false" size="normal">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" placeholder="请输入" />
          </el-form-item>
        </el-form>

        <span slot="footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="btnOk">确认</el-button>
        </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, addRole, updateRole, deleteRole } from '@/api/settings'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      page: {
        page: 1,
        pagesize: 2,
        total: 1
      },
      list: [],
      formData: {},
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名不能为空', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在3到6', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在3到20', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    //   获取用户列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    currentChange(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    sizeChange(newPage) {
      // newPage是当前点击的页码
      this.page.pagesize = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },

    async btnOk() {
      try {
        await this.$refs.roleForm.validate()
      } catch (error) {
        return console.log(error)
      }
      if (this.formData.id) {
        await updateRole(this.roleForm)
        this.$message.success('修改成功')
      } else {
        await addRole(this.roleForm)
        this.$message.success('添加成功')
      }

      this.showDialog = false
      this.getRoleList()
    },
    reset() {
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    editRole(val) {
      console.log(val)
      this.roleForm = { ...val }
      this.showDialog = true
    },
    async delRole(id) {
      try {
        await this.$confirm('确认删除？')
      } catch (error) {
        return console.log(error)
      }
      await deleteRole(id)
      this.$message.success('删除成功！')
      this.getRoleList()
    }

  }

}
</script>

<style>

</style>
